package com.xz.group12306.business.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xz.group12306.business.dao.BaseDao;
import com.xz.group12306.business.dao.UserDao;
import com.xz.group12306.domain.Cart;
import com.xz.group12306.domain.User;

public class UserDaoImpl extends BaseDao implements UserDao{
	private Connection conn=null;
	public UserDaoImpl(Connection conn) {
		this.setConn(conn);
	}
	//conn的get set方法
	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}
	
	/**
	 * author  胡晨倩
	 */
	//查询登录
	@Override
	public User selectUser(String username, String password, String rule) {
		// TODO Auto-generated method stub		
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		String sql="select * from tab_user where username=? and password=? and rule=?";
		Object param[]={username,password,rule};
		User user=this.executeOneUser(sql, param);
		BaseDao.closeConnection(conn);
		return user;

	}
	

	/**
	 * author  沈姚楠
	 */
	//向数据中添加注册的用户的信息
		@Override
		public int save(User user) throws SQLException {
			Connection conn=BaseDao.getConnection();
			//SQL语句
			String save_sql = "insert into tab_user(username,password,rule,realname,sex,city,cert_Type,cert,birthday,userType,content)"
					+ "values(?,?,2,?,?,2,?,?,?,?,?)";

			PreparedStatement pstmt = null;
			int row = 0;
			int idx = 1;
			try {
				//设置语句对象，SQL语句条件
				pstmt = conn.prepareStatement(save_sql);
				pstmt.setString(idx, user.getUsername());
				pstmt.setString(++idx, user.getPassword());
				//pstmt.setString(++idx, user.getRule());
				pstmt.setString(++idx, user.getRealname());
				pstmt.setString(++idx, user.getSex());
				//pstmt.setString(++idx, user.getCity());
				pstmt.setString(++idx, user.getCertType());
				pstmt.setString(++idx, user.getCert());
				pstmt.setDate(++idx,
						new java.sql.Date(user.getBirthday().getTime()));
				pstmt.setString(++idx, user.getUserType());
				pstmt.setString(++idx, user.getContent());

				row = pstmt.executeUpdate();
			} finally {
				//关闭连接
				BaseDao.closeStatement(null, pstmt);
			}
			return row;
		}
		
		

		/**
		 * author  沈姚楠
		 */
		//注册的时候查找数据库是否有该用户

		@Override
		public User allUser(User one) throws SQLException{
			// TODO Auto-generated method stub
			
				StringBuffer sql=new StringBuffer();
				sql.append("select username from tab_user where");
				//查询条件标记
				boolean tag = false;
			
				//查询条件username字段
				String username = one.getUsername();
				if(username!=null && !username.isEmpty()){
					sql.append("username='"+username+"'");
					tag = true;
				}		
				//若没有查询条件则返回对象为null
				if(!tag){
					return null;
				}
				
				User user = null;
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = conn.prepareStatement(sql.toString());
					rs = pstmt.executeQuery();
					if (rs.next()) {
						// 解析结果集对象，封装查询结果
						user = new User();
						user.setUsername(rs.getString("username"));

					}
				} finally {
					BaseDao.closeStatement(rs, pstmt);
			}
				return user;
		}


		/**
		 * author  胡晨倩
		 */
		//修改密码
	@Override
	public int updatePwd(String password,int id) {
		// TODO Auto-generated method stub
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		String sql="update tab_user set password=? where id=?";
		Object param[]={password,id};
		int result=this.executeUpdate(sql, param);
		BaseDao.closeConnection(conn);
		return result;
	}
	
	

	/**
	 * author  胡晨倩
	 */
	//分页查询所有的用户订单信息
	@Override
	public List<Cart> findCartByUser(User user, int page, int rows) throws SQLException {
		// TODO Auto-generated method stub
		int start=(page-1)*rows;
		List<Cart> carts=new ArrayList<Cart>();
		int userid=user.getId();

		String sql="select  *  from tab_cart where userid='"+userid+"'  limit  "+start+","+rows+"";

		        	System.out.println(sql);

		Cart cart=null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;


		try {
		pstmt=conn.prepareStatement(sql);

		rs = pstmt.executeQuery();
		while(rs.next()) {
		// 解析结果集对象，封装查询结果
		//解析结果集对象，封装查询结果
		cart=new Cart();

		cart.setUserid(rs.getInt("userid"));
		cart.setTicketid(rs.getInt("ticketid"));
		cart.setId(rs.getInt("id"));
		cart.setDepature(rs.getString("despature"));
		cart.setDestinction(rs.getString("destinction"));
		cart.setCostTime(rs.getString("costtime"));
		cart.setStartTime(rs.getDate("starttime"));
        cart.setTrainNo(rs.getString("trainno"));


		carts.add(cart);
		}
		} finally {
		BaseDao.closeStatement(rs, pstmt);
		}
	 return carts;
	}
	
	

	/**
	 * author  沈姚楠
	 */
	//修改用户信息
	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		Connection conn=BaseDao.getConnection();
		String sql="update tab_user set username='"+user.getUsername()+"',content='"+user.getContent()+"' where id='"+user.getId()+"'";
		PreparedStatement pstmt=null;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.executeUpdate();
			BaseDao.closeConnection(conn);
			System.out.println("修改用户信息成功！");
			System.out.println(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
}
